package com.ny.utils;

import org.springframework.stereotype.Component;

@Component
public class PasswordUtils {
    
    /**
     * 使用SHA-256算法对密码进行加密
     * @param password 明文密码
     * @return 加密后的密码
     */
    public static String encryptPassword(String password) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-256");
            byte[] hash = md.digest(password.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte b : hash) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("密码加密失败", e);
        }
    }
    
    /**
     * 验证密码是否正确
     * @param plainPassword 明文密码
     * @param encryptedPassword 加密后的密码
     * @return 是否匹配
     */
    public static boolean verifyPassword(String plainPassword, String encryptedPassword) {
        return encryptPassword(plainPassword).equals(encryptedPassword);
    }
}